package com.controller;

import cn.hutool.crypto.Mode;
import com.entity.BGoods;
import com.entity.BType;
import com.entity.table.BGoodsTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.impl.BGoodsServiceImpl;
import com.service.impl.BTypeServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Resource
    BGoodsServiceImpl bGoodsService;
    @Resource
    BTypeServiceImpl bTypeService;

    @GetMapping(value = "")
    public String index(Model model){
        try {
            Map<String, List<BGoods>> goodsList=new LinkedHashMap<>();
            //查询商品分类
            QueryWrapper wrapper=QueryWrapper.create().limit(6);//limit为渲染页面的六项数据
            List<BType> typeList=bTypeService.getMapper().selectListByQuery(wrapper);
            for (BType bType:typeList){
                int typeId=bType.getId();
                String typeName=bType.getTypeName();
                //查询商品列表
                wrapper=QueryWrapper.create().where(BGoodsTableDef.B_GOODS.TYPE_ID.eq(typeId)).limit(4);
                List<BGoods> goods=bGoodsService.list(wrapper);
                goodsList.put(typeName,goods);
            }
            model.addAttribute("goodType",typeList);
            model.addAttribute("goodsList",goodsList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "client/index";
    }
    @GetMapping("/list")
    public String goodList(Model model,
                           @RequestParam(value = "goodName", defaultValue = "") String goodName,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pagesize", defaultValue = "10") int pagesize) {
        try {
            // 1. 查询商品分类列表
            List<BType> typeList = bTypeService.list();

            QueryWrapper queryWrapper = QueryWrapper.create().where(BGoodsTableDef.B_GOODS.GOOD_NAME.like(goodName));
            Page<BGoods> page = new Page<>();
            page.setPageSize(pagesize);
            page.setPageNumber(p);
            // 3. 查询商品列表（含分页）
            Page<BGoods> pageInfo =bGoodsService.getMapper().paginateWithRelations(page, queryWrapper);

            // 4. 返回商品数据给前端
            model.addAttribute("page", pageInfo);

            // 5. 返回商品类型数据给前端
            model.addAttribute("goodType", typeList);
            return "client/list";

        } catch (Exception e) {
            e.printStackTrace();
            // 处理异常，例如返回错误页面
            return "client/login";
        }

    }
    //添加修改商品页面代码
    @GetMapping(value = "goodDetail")
    public String goodDetail(int id,Model model){
        try {
            //1、查询商品分类的列表
            List<BType> typeList = bTypeService.list();
            //2、根据id查询商品
            BGoods bGoods = bGoodsService.getById(id);
            //3、返回商品类型数据给前端
            model.addAttribute("goodType", typeList);
            //4、返回商品数据给前端
            model.addAttribute("bGood", bGoods);
            return "client/detail";
        }catch (Exception e){
            e.printStackTrace();
            return "client/login";
        }
    }
}
